﻿#include "Fight.h"

FightView::FightView()
{

}

FightView::~FightView()
{

}

CCScene* FightView::scene()
{
	CCScene* scene = NULL;
	do 
	{
		scene = CCScene::create();
		CC_BREAK_IF(NULL == scene);

		FightView* layer = FightView::create();
		CC_BREAK_IF(NULL == layer);

		scene->addChild(layer);

	} while (0);
	return scene;
}

bool FightView::init()
{
	bool b = false;
	do 
	{
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		CCSprite* bg_sprite = CCSprite::create("zzzz.jpg");
		bg_sprite->setPosition(ccp(size.width / 2, size.height / 2));
		addChild(bg_sprite,0,TAG_BG);

		CCMenuItemImage* pMenuItemAttack = CCMenuItemImage::create("btn-play-normal.png","btn-play-selected.png",this,menu_selector(FightView::menuAttack));
		pMenuItemAttack->setPosition(ccp(50, 50));

		CCMenuItemImage* pMenuItemBack = CCMenuItemImage::create("b1.png","b2.png",this,menu_selector(InfoView::menuBackLastScene));
		pMenuItemBack->setPosition(ccp( 50, size.height - 50));

		CCMenu* pMenu = CCMenu::create(pMenuItemBack,pMenuItemAttack, NULL);
		pMenu->setPosition(ccp(0, 0));
		addChild(pMenu, 0, TAG_MENU);

		this->initCharactor();

		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bububu.mp3", true);

		b = true;
	} while (0);
	return b;
}

void FightView::initCharactor()
{
	GameEngine* gameEngine = GameEngine::getInstanse();

	Player* p = gameEngine->player;
	Player* NPC = gameEngine->NPC;


	p->initSprite("3.png");
	p->sprite->setScale(0.5f);
	p->sprite->setPosition(ccp(50, 100));
	p->label = CCLabelTTF::create("xxxx", "MarkerFelt-Thin", 30);

	CCSize size2 = p->sprite->getContentSize();
	p->label->setPosition(ccp(size2.width / 2, size2.height + 40));
	p->sprite->addChild(p->label, 1, 1);
	this->addChild(p->sprite);

	float chooseEnemy = CCRANDOM_0_1() * 10;
	if(chooseEnemy < 7)
	{
		NPC->label = CCLabelTTF::create("smoll lolo", "MarkerFelt-Thin", 30);
		NPC->initSprite("1.png");
		string name = "SmollLoLo";
		NPC->setName(name);
	}
	else
	{
		NPC->label = CCLabelTTF::create("Keng die big bos", "MarkerFelt-Thin", 30);
		NPC->initSprite("2.png");
		string name = "¿Óµù´óboss";
		NPC->setName(name);
		NPC->setLevel(20);
		NPC->setHP(300);
		NPC->setMagicDefenceValue(22);
		NPC->setMagicHurtValue(50);
		NPC->setPhysicalDefenceValue(40);
		NPC->setPhysicalHurtValue(40);
		NPC->setAssets(2000);
	}

	NPC->sprite->setScale(0.5f);
	NPC->sprite->setPosition(ccp(200, 350));
	size2 = NPC->sprite->getContentSize();
	NPC->label->setPosition(ccp(size2.width / 2, size2.height + 40));
	NPC->sprite->addChild(NPC->label, 1, 1);
	this->addChild(NPC->sprite);


}

void FightView::ccTouchesBegan(CCSet* pTouch, CCEvent* pEvent)
{

}

void FightView::ccTouchesMoved(CCSet* pTouch, CCEvent* pEvent)
{

}

void FightView::ccTouchesEnded(CCSet* pTouch, CCEvent* pEvent)
{

}

void FightView::menuBackLastScene(CCObject* pSender)
{
	CCScene* scene = MainView::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.5f, scene, ccWHITE));
}

void FightView::menuAttack(CCObject* pSender)
{

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("hecheng.mp3", false);

	Player* NPC = GameEngine::getInstanse()->NPC;
	Player* p = GameEngine::getInstanse()->player;

	float f_attack_type = CCRANDOM_0_1() * 10;
	float f_attack_type2 = CCRANDOM_0_1() * 10;

	short attack_type;


	if(f_attack_type < 5)
	{
		attack_type = PHYSICALATTACK;
	}
	else
	{
		attack_type = MAGICATTACK;
	}

	int hurt = p->Attack(NPC, attack_type);
	this->hurtEffect(hurt, NPC->sprite);
	this->FightEffect(p, NPC, attack_type, 0.0f);

	if(f_attack_type2 < 5)
	{
		attack_type = PHYSICALATTACK;
	}
	else
	{
		attack_type = MAGICATTACK;
	}
	//delay(1700);

	hurt = NPC->Attack(p, attack_type);
	this->hurtEffect(hurt, p->sprite);
	this->FightEffect(NPC, p, attack_type, 2.0f);

	if(p->getHP() <= 0)
	{
		p->setAliveStatus(false);
		this->playerLoseEffect();
		GameEngine::getInstanse()->GameResultProcess(NPC, p);
	}
	if(NPC->getHP() <= 0)
	{
		NPC->setAliveStatus(false);
		this->playerWinEffect();
		GameEngine::getInstanse()->GameResultProcess(p, NPC);
	}

}

void FightView::stopBurn()
{
	//      unschedule(schedule_selector(FightView::stopBurn));
	Player* NPC = GameEngine::getInstanse()->NPC;
	NPC->sprite->removeChildByTag(2,true);
	NPC->sprite->removeChildByTag(5, true);

	Player* p = GameEngine::getInstanse()->player;
	p->sprite->removeChildByTag(2, true);
	p->sprite->removeChildByTag(5, true);

}

void FightView::FightEffect(Player* attack, Player* defence, short attack_type, float ccdt)
{
	int chooseStr = (int)(CCRANDOM_0_1() * 10);
	int chooseStr2 = (int)(CCRANDOM_0_1() * 10);


	attack->label = (CCLabelTTF*)attack->sprite->getChildByTag(1);
	attack->label->setString(someSentence[chooseStr].c_str());


	defence->label = (CCLabelTTF*)defence->sprite->getChildByTag(1);
	defence->label->setString(someSentence[chooseStr2].c_str());


	CCParticleFire* fire = CCParticleFire::create();
	defence->sprite->addChild(fire, 2, 2);
	//fire->setVisible(false);
	fire->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	fire->setPosition(ccp(150, 70));

	//CCActionInterval* burn = CCActionInterval::create(2.0f);
	CCScaleTo* ccst = CCScaleTo::actionWithDuration(1.0f, 2.0f, 2.0f);
	CCScaleTo* ccst2 = CCScaleTo::actionWithDuration(1.0f, 1.0f, 1.0f);

	CCDelayTime* ccdt2 = CCDelayTime::create(ccdt);

	CCFiniteTimeAction* ccfta = CCSequence::actions(ccst, ccst2, CCCallFunc::actionWithTarget(this, callfunc_selector(FightView::stopBurn)), NULL);
	//CCFiniteTimeAction* ccfta2 = CCSequence::actions(ccdt2, ccst, ccst2, NULL);
	CCDelayTime* dt = CCDelayTime::actionWithDuration(2.0f);

	fire->runAction(ccfta);
	//fire->runAction(ccfta2);

}

void FightView::playerLoseEffect()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	ccColor4B color;
	color.a = 400;
	color.b = 69;
	color.g = 69;
	color.r = 169;
	CCLayerColor* layerColor = CCLayerColor::create(color);
	layerColor->changeWidthAndHeight(size.width, 350.0f);
	//layerColor->setAnchorPoint(ccp(layerColor->getContentSize().width / 2, layerColor->getContentSize().height / 2));
	//layerColor->setAnchorPoint(ccp(size.width / 2, 350 / 2));
	//layerColor->setPosition(ccp(size.width / 2,150));
	layerColor->setPosition(ccp(0, 100));
	this->addChild(layerColor, 1);
	CCSize size2 = layerColor->getContentSize();

	CCScaleTo* ccst = CCScaleTo::create(1.0f, 1.0f, 1.0f);
	layerColor->runAction(ccst);

	CCSprite* loseTitleSprite = CCSprite::create("youlose.png");
	loseTitleSprite->setPosition(ccp(size2.width / 2, 250));
	loseTitleSprite->setScale(0.5f);
	ccColor3B color3b;
	layerColor->addChild(loseTitleSprite, 3);

	char* buffer = new char[64];
	Player* NPC = GameEngine::getInstanse()->NPC;
	Player* p = GameEngine::getInstanse()->player;

	sprintf(buffer, "Assets: - %d", LOSE_ASSETS);
	CCLabelTTF* getAssetsLabel = CCLabelTTF::create(buffer, "Helvetica-BoldOblique",30 );
	getAssetsLabel->setPosition(ccp(size2.width / 2, -500));
	color3b.b = 200;
	color3b.g = 200;
	color3b.r = 100;
	getAssetsLabel->setColor(color3b);
	layerColor->addChild(getAssetsLabel, 3);

	sprintf(buffer, "Experience: + %d",0);
	CCLabelTTF* getExperienceLabel = CCLabelTTF::create(buffer, "Helvetica-BoldOblique",30 );
	getExperienceLabel->setPosition(ccp(size2.width / 2, -500));
	color3b.b = 200;
	color3b.g = 200;
	color3b.r = 100;
	getExperienceLabel->setColor(color3b);
	layerColor->addChild(getExperienceLabel, 3);

	CCMenuItemImage* okItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(InfoView::menuBackLastScene));
	okItem->setPosition(ccp( size.width / 2, 50));

	CCMenu* pMenu = CCMenu::create(okItem,NULL);
	pMenu->setPosition(ccp(0, 0));
	layerColor->addChild(pMenu, 0, TAG_MENU);

	CCMoveTo* ccmt1 = CCMoveTo::create(0.5f, ccp(size2.width / 2, 200));
	CCMoveTo* ccmt2 = CCMoveTo::create(0.5f, ccp(size2.width / 2, 150));

	CCDelayTime* ccdt = CCDelayTime::create(0.5f);
	CCFiniteTimeAction* ccfta = CCSequence::actions(ccdt , ccmt2, NULL);

	getAssetsLabel->runAction(ccmt1);
	getExperienceLabel->runAction(ccfta); 

	CCScaleTo* ccsc1 = CCScaleTo::create(0.2f, 1.1f, 1.1f);
	CCScaleTo* ccsc2 = CCScaleTo::create(0.2f, 0.5f, 0.5f);
	CCScaleTo* ccsc3 = CCScaleTo::create(0.3f, 1.1f, 1.1f);
	CCScaleTo* ccsc4 = CCScaleTo::create(0.3f, 0.5f, 0.5f);

	ccfta = CCSequence::actions(ccsc1,ccsc2,ccsc3,ccsc4,NULL);
	loseTitleSprite->runAction(ccfta);

	CCParticleSnow* snow = CCParticleSnow::create();
	snow->setTexture(CCTextureCache::sharedTextureCache()->addImage("snow.png"));
	snow->setPosition(ccp(size.width / 2, size.height));
	snow->setSpeed(200.0f);
	this->addChild(snow, 0);

	this->removeChildByTag(TAG_MENU, true);

	delete[] buffer;

}

void FightView::playerWinEffect()
{
	//CCParticleGalaxy* ccpg = CCParticleGalaxy::create();
	//ccpg->setTexture(CCTextureCache::sharedTextureCache()->addImage("stars2.png"));
	//ccpg->setPosition(ccp(150, 70));
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	ccColor4B color;
	color.a = 400;
	color.b = 200;
	color.g = 69;
	color.r = 69;
	CCLayerColor* layerColor = CCLayerColor::create(color);
	layerColor->changeWidthAndHeight(size.width, 350.0f);
	//layerColor->setAnchorPoint(ccp(layerColor->getContentSize().width / 2, layerColor->getContentSize().height / 2));
	//layerColor->setAnchorPoint(ccp(size.width / 2, 350 / 2));
	//layerColor->setPosition(ccp(size.width / 2,150));
	layerColor->setPosition(ccp(0, 100));
	this->addChild(layerColor, 1);
	CCSize size2 = layerColor->getContentSize();

	CCParticleSnow* snow = CCParticleSnow::create();
	this->addChild(snow, 0);
	snow->setTexture(CCTextureCache::sharedTextureCache()->addImage("stars2.png"));
	snow->setPosition(ccp(size.width / 2, size.height));
	snow->setSpeed(100.0f);
	CCActionInterval* burn = CCActionInterval::create(2.0f);
	snow->runAction(burn);

	CCMenuItemImage* okItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(InfoView::menuBackLastScene));
	okItem->setPosition(ccp( size.width / 2, 50));

	CCMenu* pMenu = CCMenu::create(okItem,NULL);
	pMenu->setPosition(ccp(0, 0));
	layerColor->addChild(pMenu, 0, TAG_MENU);

	CCSprite* winTitleSprite = CCSprite::create("youwin.png");
	winTitleSprite->setPosition(ccp(size2.width / 2, 250));
	winTitleSprite->setScale(0.5f);
	ccColor3B color3b;
	color3b.b = 233;
	color3b.g = 243;
	color3b.r = 234;
	winTitleSprite->setColor(color3b);
	layerColor->addChild(winTitleSprite, 3);

	char* buffer = new char[64];
	Player* NPC = GameEngine::getInstanse()->NPC;
	Player* p = GameEngine::getInstanse()->player;

	sprintf(buffer, "Assets: + %d", WIN_ASSETS);
	CCLabelTTF* getAssetsLabel = CCLabelTTF::create(buffer, "Helvetica-BoldOblique",30 );
	getAssetsLabel->setPosition(ccp(size2.width / 2, -500));
	color3b.b = 200;
	color3b.g = 200;
	color3b.r = 100;
	getAssetsLabel->setColor(color3b);
	layerColor->addChild(getAssetsLabel, 3);

	sprintf(buffer, "Experience: + %d", WIN_EXPERIENCE_BASIC * NPC->getLevel() * NPC->getLevel());
	CCLabelTTF* getExperienceLabel = CCLabelTTF::create(buffer, "Helvetica-BoldOblique",30 );
	getExperienceLabel->setPosition(ccp(size2.width / 2, -500));
	color3b.b = 200;
	color3b.g = 200;
	color3b.r = 100;
	getExperienceLabel->setColor(color3b);
	layerColor->addChild(getExperienceLabel, 3);

	CCMoveTo* ccmt1 = CCMoveTo::create(0.5f, ccp(size2.width / 2, 200));
	CCMoveTo* ccmt2 = CCMoveTo::create(0.5f, ccp(size2.width / 2, 150));

	CCDelayTime* ccdt = CCDelayTime::create(0.5f);
	CCFiniteTimeAction* ccfta = CCSequence::actions(ccdt , ccmt2, NULL);

	getAssetsLabel->runAction(ccmt1);
	getExperienceLabel->runAction(ccfta); 

	CCScaleTo* ccsc1 = CCScaleTo::create(0.2f, 1.1f, 1.1f);
	CCScaleTo* ccsc2 = CCScaleTo::create(0.2f, 0.5f, 0.5f);
	CCScaleTo* ccsc3 = CCScaleTo::create(0.3f, 1.1f, 1.1f);
	CCScaleTo* ccsc4 = CCScaleTo::create(0.3f, 0.5f, 0.5f);

	ccfta = CCSequence::actions(ccsc1,ccsc2,ccsc3,ccsc4,NULL);
	winTitleSprite->runAction(ccfta);

	this->removeChildByTag(TAG_MENU, true);

	delete[] buffer;
}

void FightView::hurtEffect(int hurtValue, CCSprite* sprite)
{
	char* buffer = new char[64];

	sprintf(buffer, "- %d", hurtValue);
	CCLabelTTF* hurtValueLabel = CCLabelTTF::create(buffer, "Helvetica-BoldOblique", 100);

	ccColor3B color;
	color.r = 255;
	color.g = 80;
	color.b = 70;

	hurtValueLabel->setColor(color);

	CCSize size = sprite->getContentSize();
	hurtValueLabel->setPosition(ccp(size.width / 2, size.height / 2 + 100));

	CCMoveTo* ccmt1 = CCMoveTo::create(0.5f, ccp(size.width / 2 , size.height / 2 + 140));
	CCScaleTo* ccst1 = CCScaleTo::create(0.5f, 1.3f, 1.3f);

	hurtValueLabel->runAction(ccmt1);
	hurtValueLabel->runAction(ccst1);

	sprite->addChild(hurtValueLabel, 5, 5);

	delete[] buffer;
}
